package engine.graphics;

import misc.Util;
import util.List;
import engine.GameEngine;
import engine.TimerThread;
import engine.UnitAction;
import engine.data.City;
import engine.data.Player;
import engine.data.Unit;
import engine.data.UnitType;
import engine.data.factories.UnitFactory;
import engine.graphics.popup.PopupManager;
import engine.step.BeginPlayerTurnStep;
import engine.step.Step;

public final class MapCanvasEngine {

	public boolean selectionMode = false;

	public static MapCanvas canvas;

	public Unit cursor = UnitFactory.getUnit(null, UnitType.CURSOR);

	private static TimerThread thread;

	public void start() {

		// TEST
		GameEngine.initTestPlayers();

		// TEST
		GameEngine.initTest();

		Step.nextTurnAction(BeginPlayerTurnStep.getStep());
		thread = new TimerThread();
	}

	public void beginTurn() {
		List objects = GameEngine.currPlayer.units;
		for (short i = 0; i < objects.size(); i++)
			canvas.refreshUnitRange((Unit) objects.get(i));
		objects = GameEngine.currPlayer.cities;
		for (short i = 0; i < objects.size(); i++)
			canvas.refreshCityRange((City) objects.get(i));
	}

	public void addAction(final byte action) {
		if (thread != null)
			thread.addAction(action);
	}

	public void setPlayerCurrUnit(final Unit unit) {
		GameEngine.setCurrUnit(unit);
		if (GameEngine.currPlayer.showOnScr)
			canvas.autocenter(unit);
	}

	public void refreshUnitRange(final Unit unit) {
		canvas.refreshUnitRange(unit);
	}

	public void autocenter() {
		if (GameEngine.currPlayer.showOnScr)
			canvas.autocenter(GameEngine.currPlayer.currUnit);
	}

	public void moveUnit(final byte direction) {
		Player player = GameEngine.currPlayer;
		Unit unit = player.currUnit;
		short dX = Util.computeDeltaX(direction);
		short dY = Util.computeDeltaY(player.currUnit, GameEngine.map, direction);
		if (GameEngine.moveUnitHide(dX, dY)) {
			canvas.refreshUnitRange(unit);
			canvas.moveUnit(player, unit, dX, dY);
			GameEngine.moveUnitShow(dX, dY);
			canvas.refreshUnitRange(unit);
		}
	}

	public void doAction(final byte action) {
		if (action == UnitAction.FIRE) {
			if (!this.selectionMode)
				PopupManager.showPopupUnitOrders();
			else {
				this.hideCursor();
				if (GameEngine.map.getTile(cursor.x, cursor.y).image != null)
					PopupManager.showPopupTerrain(cursor);
				else
					PopupManager.popupDisplayed = false;
			}
		} else if (!selectionMode) {
			autocenter();
			GameEngine.doUnitAction(new UnitAction(action));
		} else
			canvas.moveCursor(cursor, Util.computeDeltaX(action), Util.computeDeltaY(GameEngine.currPlayer.currUnit,
					GameEngine.map, action));
	}

	public void showUnit() {
		if (!PopupManager.popupDisplayed && !selectionMode && GameEngine.currPlayer != null
				&& GameEngine.currPlayer.currUnit != null)
			canvas.showUnit(GameEngine.currPlayer.currUnit);
	}

	public void hideUnit() {
		if (!PopupManager.popupDisplayed && !selectionMode && GameEngine.currPlayer != null
				&& GameEngine.currPlayer.currUnit != null)
			canvas.hideUnit(GameEngine.currPlayer.currUnit);
	}

	public void showCursor(final short x, final short y) {
		selectionMode = true;
		cursor.x = x;
		cursor.y = y;
		canvas.autocenter(cursor);
		canvas.showCursor(cursor);
	}

	public void hideCursor() {
		this.selectionMode = false;
		canvas.hideCursor(cursor);
		autocenter();
	}

	public void refreshRange(final short x, final short y, final byte r) {
		canvas.refreshRange(x, y, r);
	}

}
